1 using UnityEngine;
2
3 ///
<summary>
4 ///
Very basic component to move a GameObject by WASD and Space.
5 ///
</summary>
6 ///
<remarks>
7 ///
Requires a PhotonView.
8 ///
Disables itself on GameObjects that are not owned on Start.
9 ///

10 ///
Speed affects movement-speed.
11 ///
JumpForce defines how high the object "jumps".
12 ///
JumpTimeout defines after how many seconds you can jump again.
13 ///
</remarks>
14 [RequireComponent(
typeof (PhotonView))]
15 public
class MoveByKeys : Photon.MonoBehaviour
16 {
17     
public float Speed = 10f;
18     
public float JumpForce = 200f;
19     
public float JumpTimeout = 0.5f;
20
21     
private bool isSprite;
22     
private float jumpingTime;
23     
private Rigidbody body;
24     
private Rigidbody2D body2d;
25
26     
public void Start()
27     {
28         
//enabled = photonView.isMine;
29         
this.isSprite = (GetComponent<SpriteRenderer>() != null);
30
31         
this.body2d = GetComponent<Rigidbody2D>();
32         
this.body = GetComponent<Rigidbody>();
33     }
34
35
36     
// Update is called once per frame
37     
public void FixedUpdate()
38     {
39         
if (!photonView.isMine)
40         {
41             
return;
42         }
43
44         
if (Input.GetKey(KeyCode.A))
45         {
46             transform.position += Vector3.left*(
this.Speed*Time.deltaTime);
47         }
48
49         
if (Input.GetKey(KeyCode.D))
50         {
51             transform.position += Vector3.right*(
this.Speed*Time.deltaTime);
52         }
53
54         
// jumping has a simple "cooldown" time but you could also jump in the air
55         
if (this.jumpingTime <= 0.0f)
56         {
57             
if (this.body != null || this.body2d != null)
58             {
59                 
// obj has a Rigidbody and can jump (AddForce)
60                 
if (Input.GetKey(KeyCode.Space))
61                 {
62                     
this.jumpingTime = this.JumpTimeout;
63
64                     Vector2 jump = Vector2.up*
this.JumpForce;
65                     
if (this.body2d != null)
66                     {
67                         
this.body2d.AddForce(jump);
68                     }
69                     
else if (this.body != null)
70                     {
71                         
this.body.AddForce(jump);
72                     }
73                 }
74             }
75         }
76         
else
77         {
78             
this.jumpingTime -= Time.deltaTime;
79         }
80
81         
// 2d objects can't be moved in 3d "forward"
82         
if (!this.isSprite)
83         {
84             
if (Input.GetKey(KeyCode.W))
85             {
86                 transform.position += Vector3.forward*(
this.Speed*Time.deltaTime);
87             }
88
89             
if (Input.GetKey(KeyCode.S))
90             {
91                 transform.position -= Vector3.forward*(
this.Speed*Time.deltaTime);
92             }
93         }
94     }
95 }


Very basic component to move a GameObject by WASD and Space.

Requires a PhotonView.

Disables itself on GameObjects that are not owned on Start.

Speed affects movement-speed.

JumpForce defines how high the object "jumps".

JumpTimeout defines after how many seconds you can jump again.

enabled = photonView.isMine;

Update is called once per frame

jumping has a simple "cooldown" time but you could also jump in the air

obj has a Rigidbody and can jump (AddForce)

2d objects can't be moved in 3d "forward"




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.567 lượt xem

Gõ tìm kiếm nhanh...